package com.jiujia.game.sdk;

import android.app.Activity;
import android.app.Application;

import com.jiujia.game.sdk.base.JGameActivityManager;
import com.jiujia.game.sdk.base.JGameContextManager;
import com.jiujia.game.sdk.base.core.callback.JGameSdkCallback;
import com.jiujia.game.sdk.base.core.model.JGameOrderInfo;
import com.jiujia.game.sdk.base.core.model.JGameRoleInfo;
import com.jiujia.game.sdk.base.core.model.JGameSdkPayInfo;
import com.jiujia.game.sdk.base.core.model.JGameSdkUserInfo;
import com.jiujia.game.sdk.base.util.JGameLog;
import com.jiujia.game.sdk.login.api.ILoginService;
import com.jiujia.game.sdk.login.api.LoginCallback;
import com.jiujia.game.sdk.login.internal.JGameLoginService;
import com.jiujia.game.sdk.pay.api.IPayService;
import com.jiujia.game.sdk.pay.api.PayCallback;
import com.jiujia.game.sdk.pay.internal.JGamePayService;
import com.jiujia.game.sdk.report.api.IReportService;
import com.jiujia.game.sdk.report.internal.JGameReportService;

/**
 * *************************************************
 * Class: GameSdk
 * Author: Jiujia
 * Time: 2025/5/2 16:02
 * Description: SDK对外入口
 * *************************************************
 */
public class JGameSdk {
    private static JGameSdk instance;
    // 回调接口
    private JGameSdkCallback mJGameSdkCallback;
    // 登录服务
    private final ILoginService mILoginService;
    // 支付服务
    private final IPayService mIPayService;
    // 汇报服务
    private final IReportService mIReportService;

    // 私有化构造方法，强制通过getInstance()获取
    private JGameSdk() {
        mILoginService = new JGameLoginService();
        mIPayService = new JGamePayService();
        mIReportService = new JGameReportService();

    }

    // 获取单例（需传入Application Context避免内存泄漏）
    public static synchronized JGameSdk getInstance() {
        if (instance == null) {
            instance = new JGameSdk();
        }
        return instance;
    }

    // 提供静态初始化方法（给不继承JGameSdkApplication的用户）
    public static class JGameApplication {
        public static void init(Application app) {
            JGameLog.i("------------- JGameSdk.JGameApplication.init(this) -------------");
            JGameContextManager.init(app);
        }
    }

    // 对外暴露的API

    public void init(Activity mActivity, JGameSdkCallback callback) {
        mJGameSdkCallback = callback;
        JGameActivityManager.init(mActivity);
        mILoginService.init();
    }

    public void login() {
        mILoginService.login(new LoginCallback() {
            @Override
            public void onSuccess(JGameSdkUserInfo mJGameSdkUserInfo) {
                if (mJGameSdkCallback != null) {
                    mJGameSdkCallback.onLoginSuccess(new JGameSdkUserInfo(mJGameSdkUserInfo.getUserId(), mJGameSdkUserInfo.getToken()));
                }
            }

            @Override
            public void onFailed(String error) {
                if (mJGameSdkCallback != null) {
                    mJGameSdkCallback.onLoginFailed(error);
                }
            }
        });
    }

    public void pay(JGameOrderInfo mJGameOrderInfo, JGameRoleInfo mJGameRoleInfo) {

        mIPayService.pay(mJGameOrderInfo, mJGameRoleInfo, new PayCallback() {

            @Override
            public void onSuccess(JGameSdkPayInfo mJGameSdkPayInfo) {
                if (mJGameSdkCallback != null) {
                    mJGameSdkCallback.onPaySuccess(new JGameSdkPayInfo(mJGameSdkPayInfo.getOrderId()));
                }
            }

            @Override
            public void onFailed(String error) {
                if (mJGameSdkCallback != null) {
                    mJGameSdkCallback.onPayFailed(error);
                }

            }
        });
    }

    public void report(JGameRoleInfo mJGameRoleInfo) {
        mIReportService.report(mJGameRoleInfo);
    }
}
